package com.tree.backend.service.impl;

import com.tree.backend.entity.vo.dashboard.*;
import com.tree.backend.service.IDashboardService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Dashboard服务实现类
 */
@Service
public class DashboardServiceImpl implements IDashboardService {

    @Override
    public DashboardStatsVO getDashboardStats() {
        DashboardStatsVO statsVO = new DashboardStatsVO();
        statsVO.setTotalExpense(128500515000.50);
        statsVO.setMonthlyExpense(156800.75);
        statsVO.setDepartmentCount(8);
        statsVO.setCostItemCount(52);
        statsVO.setPendingBudgetCount(3);
        return statsVO;
    }

    @Override
    public DepartmentCostDistributionVO getDepartmentCostDistribution() {
        DepartmentCostDistributionVO distributionVO = new DepartmentCostDistributionVO();
        
        // 设置部门名称和成本数据
        distributionVO.setDepartmentNames(Arrays.asList("研发部", "市场部", "销售部", "人力资源部", "财务部", "行政部", "运维部", "客服部"));
        distributionVO.setCostData(Arrays.asList(350000.00, 220000.00, 180000.00, 120000.00, 95000.00, 80000.00, 65000.00, 45000.00));
        
        // 设置详情列表
        List<DepartmentCostDistributionVO.DepartmentCostDetail> details = new ArrayList<>();
        details.add(createDepartmentCostDetail(1L, "研发部", 350000.00, 35.0));
        details.add(createDepartmentCostDetail(2L, "市场部", 220000.00, 22.0));
        details.add(createDepartmentCostDetail(3L, "销售部", 180000.00, 18.0));
        details.add(createDepartmentCostDetail(4L, "人力资源部", 120000.00, 12.0));
        details.add(createDepartmentCostDetail(5L, "财务部", 95000.00, 9.5));
        details.add(createDepartmentCostDetail(6L, "行政部", 80000.00, 8.0));
        details.add(createDepartmentCostDetail(7L, "运维部", 65000.00, 6.5));
        details.add(createDepartmentCostDetail(8L, "客服部", 45000.00, 4.5));
        
        distributionVO.setDetails(details);
        return distributionVO;
    }

    @Override
    public CostItemAnalysisVO getCostItemAnalysis() {
        CostItemAnalysisVO analysisVO = new CostItemAnalysisVO();
        
        // 设置成本项目名称和金额数据
        analysisVO.setCostItemNames(Arrays.asList("薪资福利", "办公设备", "差旅费", "培训费", "会议费", "营销推广", "水电费", "通信费", "订阅费", "其他"));
        analysisVO.setCostAmounts(Arrays.asList(520000.00, 180000.00, 120000.00, 95000.00, 80000.00, 75000.00, 65000.00, 45000.00, 35000.00, 25000.00));
        
        // 设置详情列表
        List<CostItemAnalysisVO.CostItemDetail> details = new ArrayList<>();
        details.add(createCostItemDetail(1L, "薪资福利", 520000.00, 41.6, 120));
        details.add(createCostItemDetail(2L, "办公设备", 180000.00, 14.4, 35));
        details.add(createCostItemDetail(3L, "差旅费", 120000.00, 9.6, 65));
        details.add(createCostItemDetail(4L, "培训费", 95000.00, 7.6, 20));
        details.add(createCostItemDetail(5L, "会议费", 80000.00, 6.4, 40));
        details.add(createCostItemDetail(6L, "营销推广", 75000.00, 6.0, 15));
        details.add(createCostItemDetail(7L, "水电费", 65000.00, 5.2, 12));
        details.add(createCostItemDetail(8L, "通信费", 45000.00, 3.6, 8));
        details.add(createCostItemDetail(9L, "订阅费", 35000.00, 2.8, 6));
        details.add(createCostItemDetail(10L, "其他", 25000.00, 2.0, 18));
        
        analysisVO.setDetails(details);
        return analysisVO;
    }

    @Override
    public CostTrendAnalysisVO getCostTrendAnalysis() {
        CostTrendAnalysisVO trendVO = new CostTrendAnalysisVO();
        
        // 设置时间点和成本数据
        trendVO.setTimePoints(Arrays.asList("1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"));
        trendVO.setCostData(Arrays.asList(85000.00, 92000.00, 105000.00, 110000.00, 120000.00, 135000.00, 145000.00, 156000.00, 165000.00, 156800.00, 0.0, 0.0));
        
        // 设置趋势详情
        List<CostTrendAnalysisVO.TrendDetail> details = new ArrayList<>();
        details.add(createTrendDetail("1月", 85000.00, 0.0));
        details.add(createTrendDetail("2月", 92000.00, 8.24));
        details.add(createTrendDetail("3月", 105000.00, 14.13));
        details.add(createTrendDetail("4月", 110000.00, 4.76));
        details.add(createTrendDetail("5月", 120000.00, 9.09));
        details.add(createTrendDetail("6月", 135000.00, 12.5));
        details.add(createTrendDetail("7月", 145000.00, 7.41));
        details.add(createTrendDetail("8月", 156000.00, 7.59));
        details.add(createTrendDetail("9月", 165000.00, 5.77));
        details.add(createTrendDetail("10月", 156800.00, -5.0));
        details.add(createTrendDetail("11月", 0.0, 0.0));
        details.add(createTrendDetail("12月", 0.0, 0.0));
        
        trendVO.setDetails(details);
        return trendVO;
    }

    @Override
    public MonthlyExpenseComparisonVO getMonthlyExpenseComparison() {
        MonthlyExpenseComparisonVO comparisonVO = new MonthlyExpenseComparisonVO();
        
        // 设置月份和对比数据
        comparisonVO.setMonths(Arrays.asList("1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"));
        comparisonVO.setCurrentYearData(Arrays.asList(85000.00, 92000.00, 105000.00, 110000.00, 120000.00, 135000.00, 145000.00, 156000.00, 165000.00, 156800.00, 0.0, 0.0));
        comparisonVO.setLastYearData(Arrays.asList(75000.00, 80000.00, 90000.00, 95000.00, 105000.00, 115000.00, 125000.00, 135000.00, 140000.00, 135000.00, 145000.00, 150000.00));
        
        // 设置对比详情
        List<MonthlyExpenseComparisonVO.MonthlyComparisonDetail> details = new ArrayList<>();
        details.add(createMonthlyComparisonDetail("1月", 85000.00, 75000.00, 13.33));
        details.add(createMonthlyComparisonDetail("2月", 92000.00, 80000.00, 15.0));
        details.add(createMonthlyComparisonDetail("3月", 105000.00, 90000.00, 16.67));
        details.add(createMonthlyComparisonDetail("4月", 110000.00, 95000.00, 15.79));
        details.add(createMonthlyComparisonDetail("5月", 120000.00, 105000.00, 14.29));
        details.add(createMonthlyComparisonDetail("6月", 135000.00, 115000.00, 17.39));
        details.add(createMonthlyComparisonDetail("7月", 145000.00, 125000.00, 16.0));
        details.add(createMonthlyComparisonDetail("8月", 156000.00, 135000.00, 15.56));
        details.add(createMonthlyComparisonDetail("9月", 165000.00, 140000.00, 17.86));
        details.add(createMonthlyComparisonDetail("10月", 156800.00, 135000.00, 16.15));
        details.add(createMonthlyComparisonDetail("11月", 0.0, 145000.00, 0.0));
        details.add(createMonthlyComparisonDetail("12月", 0.0, 150000.00, 0.0));
        
        comparisonVO.setDetails(details);
        return comparisonVO;
    }
    
    // 辅助方法
    private DepartmentCostDistributionVO.DepartmentCostDetail createDepartmentCostDetail(Long id, String name, Double cost, Double percentage) {
        DepartmentCostDistributionVO.DepartmentCostDetail detail = new DepartmentCostDistributionVO.DepartmentCostDetail();
        detail.setDepartmentId(id);
        detail.setDepartmentName(name);
        detail.setCost(cost);
        detail.setPercentage(percentage);
        return detail;
    }
    
    private CostItemAnalysisVO.CostItemDetail createCostItemDetail(Long id, String name, Double amount, Double percentage, Integer count) {
        CostItemAnalysisVO.CostItemDetail detail = new CostItemAnalysisVO.CostItemDetail();
        detail.setCostItemId(id);
        detail.setCostItemName(name);
        detail.setAmount(amount);
        detail.setPercentage(percentage);
        detail.setCount(count);
        return detail;
    }
    
    private CostTrendAnalysisVO.TrendDetail createTrendDetail(String timePoint, Double cost, Double growthRate) {
        CostTrendAnalysisVO.TrendDetail detail = new CostTrendAnalysisVO.TrendDetail();
        detail.setTimePoint(timePoint);
        detail.setCost(cost);
        detail.setGrowthRate(growthRate);
        return detail;
    }
    
    private MonthlyExpenseComparisonVO.MonthlyComparisonDetail createMonthlyComparisonDetail(String month, Double currentYear, Double lastYear, Double yearOnYearGrowth) {
        MonthlyExpenseComparisonVO.MonthlyComparisonDetail detail = new MonthlyExpenseComparisonVO.MonthlyComparisonDetail();
        detail.setMonth(month);
        detail.setCurrentYearExpense(currentYear);
        detail.setLastYearExpense(lastYear);
        detail.setYearOnYearGrowth(yearOnYearGrowth);
        return detail;
    }
}